package com.example.mall.controller;

import com.alibaba.fastjson.JSON;
import com.example.mall.pojo.Goods;
import com.example.mall.pojo.NewGoodsList;
import com.example.mall.pojo.Path;
import com.example.mall.pojo.Rbody;
import com.example.mall.service.GoodsService;
import com.github.pagehelper.PageInfo;
import javafx.application.Application;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Delete;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@AllArgsConstructor
public class GoodsController {

    GoodsService goodsService;



    @PostMapping("/upload")
    public Rbody addPicture(HttpServletRequest request,MultipartFile file) throws IOException {
        Rbody rbody = new Rbody();
        Path path = new Path();
        String originalFilename = file.getOriginalFilename();
        System.out.println(originalFilename);
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        File file1 = new File(""+"uploads/" + file);
        System.out.println(file1);
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);
        ApplicationHome home = new ApplicationHome(getClass());
        File source = home.getSource();
        String dirPath=source.getParentFile().toString()+"/upload/";
        System.out.println(dirPath);
        File file2 = new File(dirPath);
        if(!file2.exists()){
            file2.mkdirs();
        }
        file.transferTo(new File(dirPath+originalFilename));
        path.setTmp_path("upload/"+originalFilename);
        path.setUrl("http://127.0.0.1:3007/"+"upload/"+originalFilename);
        rbody.setData(path);
        rbody.getMeta().setMsg("上传图片成功");
        rbody.getMeta().setStatus(200);

        return rbody;
    }

    @PostMapping("/goods")
    public Rbody addGoods(@RequestBody String body){
        Rbody rbody = new Rbody();
        System.out.println("=============" + body);
        Goods goods = JSON.parseObject(body, Goods.class);

        Goods goodsName = goodsService.getGoodsName(goods.getGoods_name());
        if(goodsName==null){

            Boolean addGoods = goodsService.addGoods(goods.getGoods_name(), goods.getGoods_price(), goods.getGoods_number(), goods.getGoods_weight(),goods.getGoods_id(),  goods.getGoods_introduce(), goods.getGoods_big_logo()
                    , goods.getGoods_small_logo(), (int) new Date().getTime(), (int) new Date().getTime(), goods.getHot_mumber(), goods.getIs_promote(), goods.getGoods_state());
            if(!addGoods){
                rbody.getMeta().setStatus(400);
                rbody.getMeta().setMsg("添加商品失败");
            }
            rbody.getMeta().setStatus(201);
            rbody.getMeta().setMsg("添加商品成功");
        }else {
            rbody.getMeta().setStatus(400);
            rbody.getMeta().setMsg("添加商品失败");
        }

        return  rbody;
    }



    @DeleteMapping("/goods/{id}")
    public Rbody deleteById(@PathVariable("id") Integer id){
        Rbody rbody = new Rbody();
        Boolean deleteById = goodsService.deleteById(id);
        if(!deleteById){
            rbody.getMeta().setStatus(400);
        }else {
            rbody.getMeta().setStatus(200);
        }
        rbody.getMeta().setMsg("删除成功");

        return rbody;
    }

    @GetMapping("/goods")
    public Rbody getGoodsList( String query,Integer pagenum,Integer pagesize){
        Rbody rbody = new Rbody();
        if(query==null){
            List<Goods> goods = goodsService.selectAll(pagenum,pagesize);
            PageInfo<Goods> pagesAll = goodsService.getPagesAll(pagenum);
            long total = pagesAll.getTotal();
            System.out.println("total:"+total);
            int pageNum = pagesAll.getPageNum();
            NewGoodsList goodsList = new NewGoodsList();
            goodsList.setGoods(goods);
            goodsList.setPagenum(pageNum);
            goodsList.setTotal((int) total);
            if(goods==null){
                rbody.getMeta().setStatus(400);
                rbody.getMeta().setMsg("获取商品失败");
            }else {
                rbody.setData(goodsList);
                rbody.getMeta().setStatus(200);
                rbody.getMeta().setMsg("获取商品成功");
            }
        }else {
            List<Goods> goods = goodsService.selectByName(query, pagenum, pagesize);
            PageInfo<Goods> pagesByName = goodsService.getPagesByName(query, pagenum);
            long total = pagesByName.getTotal();
            System.out.println("total:"+total);
            int pageNum = pagesByName.getPageNum();
            NewGoodsList goodsList = new NewGoodsList();
            goodsList.setGoods(goods);
            goodsList.setPagenum(pageNum);
            goodsList.setTotal((int) total);
            if(goods==null){
                rbody.getMeta().setStatus(400);
                rbody.getMeta().setMsg("获取商品失败");
            }else {
                rbody.setData(goodsList);
                rbody.getMeta().setStatus(200);
                rbody.getMeta().setMsg("获取商品成功");
            }
        }

        return rbody;

    }
}
